var TimeLineDragDrop = function (gantt, options) {
  this.gantt = gantt
  $.extend(this, options)
  this.init()
}
TimeLineDragDrop.prototype = {
  enableDragDrop: true,

  init: function () {
    var me = this
    var gantt = me.gantt
    var ganttView = gantt.ganttView

    ganttView.on('refresh', function (e) {
      var jq = $(ganttView.el).find('.mini-gantt-timeline')
      //            jq.css({
      //                cursor: "w-resize",
      //                overflow: 'visible'
      //            });
    })

    //        $(ganttView.el).on("click", function (e) {
    //            var jq = $(e.target).closest('.mini-gantt-timeline');
    //            if (jq[0]) {
    //                var index = parseInt(jq.attr("index"));

    //                e.stop()
    //            }
    //        });
    $(ganttView.el).bind('mousedown', function (e) {
      if (me.enableDragDrop) {
        me.startDrag(e)
      }
    })
  },

  startDrag: function (event) {
    var me = this
    var gantt = me.gantt
    var ganttView = gantt.ganttView

    var jq = $(event.target).closest('.mini-gantt-timeline')
    if (!jq[0]) return
    event.preventDefault()
    var index = parseInt(jq.attr('index'))

    var timeLine = ganttView.timeLines[index]
    var box = jq.offset()

    function onMouseMove (e) {
      var offsetX = e.pageX - event.pageX

      jq.offset({
        top: box.top,
        left: box.left + offsetX
      })
    }

    function onMouseUp (e) {
      $(document).unbind('mousemove', onMouseMove)
      $(document).unbind('mouseup', onMouseUp)

      var box = jq.offset()
      var date = ganttView.getDateByPageX(box.left)

      timeLine.date = date

      gantt.refresh()
    }

    $(document).bind('mousemove', onMouseMove)
    $(document).bind('mouseup', onMouseUp)
  }

}
